<?php

declare(strict_types=1);
/*
 * @Description: 个推service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-09-07 15:31:08
 */

namespace App\Service\File;

use App\Exception\ApiException;
use Qcloud\Cos\Client;

class TenCos
{
    protected $serverInfo = [];
    protected $ossClient;
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
        if ($this->serverInfo['tenc_is_internal_upload_bool'] == 1) {
            $this->serverInfo['Endpoint'] = $this->serverInfo['tenc_region'] . '-internal.aliyuncs.com';
        } else {
            $this->serverInfo['Endpoint'] = $this->serverInfo['tenc_region'] . '.aliyuncs.com';
        }
        $this->ossClient = new Client(array(
            'region' => $this->serverInfo['tenc_region'],
            'schema' => 'https', //协议头部，默认为http
            'credentials' => array(
                'secretId'  => $this->serverInfo['tenc_secret_id'],
                'secretKey' => $this->serverInfo['tenc_secret_key']
            )
        ));
    }
    public function uploadFile($object, $Path)
    {
        try {
            $result = $this->ossClient->upload(
                $this->serverInfo['tenc_bucket'],
                $object, //此处的 key 为对象键
                fopen($Path, 'rb')
            );
            // 请求成功
            return $result;
        } catch (\Exception $e) {
            throw new ApiException(-1, $e->getMessage());
        }
    }
    public function getFileUrl($object, $timeout = 120)
    {
        if (is_null($this->ossClient)) {
            exit(1);
        }
        if ($this->serverInfo['tenc_alc'] == 'private') {
            return $this->getOssSecureUrl($object, $timeout);
        } else {
            return $this->getOssUrl($object);
        }
    }
    public function getOssSecureUrl($object, $timeout = 120)
    {
        if (is_null($this->ossClient)) {
            exit(1);
        }
        try {
            return $this->ossClient->getObjectUrl($this->serverInfo['tenc_bucket'], $object, '+'.intval($timeout/60).' minutes');
        } catch (\Exception $e) {
            throw new ApiException(-1, $e->getMessage());
        }
    }
    public function getOssUrl($object)
    {
        if ($this->serverInfo['tenc_is_define_url_bool'] == 1) {
            $url = $this->serverInfo['tenc_web_url'];
        } else {
            $url = 'https://' . $this->serverInfo['tenc_bucket'] . '.cos.' . $this->serverInfo['tenc_region'] . '.myqcloud.com';
        }
        $url = rtrim($url, '/');
        return $url . '/' . $object;
    }
    public function deleteOssFile($object,$bucket='')
    {
        if (is_null($this->ossClient)) {
            exit(1);
        }
        try {
            if (empty($bucket)) {
                $bucket = $this->serverInfo['tenc_bucket'];
            }
            return $this->ossClient->deleteObject(array(
                'Bucket' => $bucket,
                'Key' => $object
            ));
        } catch (\Exception $e) {
            throw new ApiException(-1, $e->getMessage());
        }
    }
}
